package com.jiadakeji.ppm.common.swagger;

import io.swagger.annotations.Api;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger2的配置
 * @author liuhanjun
 * @date 2017年8月11日
 */
@Configuration    // 必须，spring管理注入
@EnableSwagger2	// 必须，启用swagger2
@EnableWebMvc    // 必须，启用springmvc
@ComponentScan("com.jiadakeji.ppm.api") // 扫描包路径
public class MySwaggerConfig {

	@Bean
	public Docket apiv1() {
        List<Parameter> pars = new ArrayList<Parameter>();
        String token = "1";
        String timestamp = System.currentTimeMillis()+"";
        String sign = DigestUtils.md5Hex(token + timestamp + "zhangfen");
        ParameterBuilder tokenPara = new ParameterBuilder();
        tokenPara.name("token").description("登录时获取token").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue(token).build();
        ParameterBuilder timestampPara = new ParameterBuilder();
        timestampPara.name("timestamp").description("时间戳").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue(timestamp).build();
        ParameterBuilder signPara = new ParameterBuilder();
        signPara.name("sign").description("MD5(token+timestamp+KEY)").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue(sign).build();
        pars.add(tokenPara.build());
        pars.add(timestampPara.build());
        pars.add(signPara.build());
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("v1")
                .select() 
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/api/v1/.*"))
                .build()
				.globalOperationParameters(pars)
				//.ignoredParameterTypes(Company.class,User.class)
				.apiInfo(apiInfo());
	}
	
	@Bean
	public Docket apiv2() {
		List<Parameter> pars = new ArrayList<Parameter>();
        String token = "1";
        String timestamp = System.currentTimeMillis()+"";
        String sign = DigestUtils.md5Hex(token + timestamp + "zhangfen");
        ParameterBuilder tokenPara = new ParameterBuilder();
        tokenPara.name("token").description("登录时获取token").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue(token).build();
        ParameterBuilder timestampPara = new ParameterBuilder();
        timestampPara.name("timestamp").description("时间戳").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue(timestamp).build();
        ParameterBuilder signPara = new ParameterBuilder();
        signPara.name("sign").description("MD5(token+timestamp+KEY)").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue(sign).build();
        pars.add(tokenPara.build());
        pars.add(timestampPara.build());
        pars.add(signPara.build());
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("v2")
				.select() 
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.paths(PathSelectors.regex("/api/v2/.*"))
				.build()
				.globalOperationParameters(pars)
				.apiInfo(apiInfo());
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("钓鱼帮接口文档")
				.description("一律需要从header中传递token，timestamp和sign=MD5(token+timestamp+KEY)。<br>返回值一律是JsonResult{code=(0表示正常，其它异常),message=消息,data=返回数据}")
				.build();
	}
}
